<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
      selectionType="single"
    >
    </FilterTable>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <el-button @click="beforeClose(2)" type="primary" :disabled='isOpen'>确 定</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { searchServiceActivities,searchDetailedBymaNo,activatedPart } from '@/api/partsmanage/aduitOrder/orderProxyReport';


export default {
  props: {
    ownerCode: {
      type: String | Number,
      default: ''
    }
  },
  data(){
    return{
      topFilterList:this.createdFrom(),
      columns:this.createdTable(),
      fetchParams:{
        ownerCode: this.ownerCode,
        onlyPart: 10041001
      },
      fetchApi:searchServiceActivities,
      selectList:[],
      isOpen:true,
    }
  },
  mixins:[dictionary],
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '活动编号',
          fieldName: 'maNo',
          placeholder: '请选择',
        },
        {
          type: 'INPUT',
          label: '活动名称',
          fieldName: 'maTitle',
          placeholder: '请选择',
        },
      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '活动编号',
          dataIndex: 'maNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '活动名称',
          dataIndex: 'maTitle',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '备注',
          dataIndex: 'maRemarks',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '生效日期',
          dataIndex: 'bDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '截至日期',
          dataIndex: 'eDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ]
    },
    changeHandle(val){
      this.isOpen=true;
      this.fetchParams=Object.assign({},this.fetchParams,val)
    },
    beforeClose(type){
          this.$confirm('确认后若之前有选其他活动则会替换', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(()=>{
            this.closeDrawer(type);
          }).catch(()=>{
            this.$message({
                type: 'warning',
                message: '已取消!'
            });
          })

    },
    async closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else{
        let selectPartList = [];
        if(this.selectList != null && this.selectList.length > 0 ){
            
            let partNoArr = [];
            let remark = '';
            for(let i = 0;i<this.selectList.length;i++){
                let activityInfo = this.selectList[i];
                remark = activityInfo.maNo + "-"+ activityInfo.maTitle
                let res1 = await searchDetailedBymaNo(activityInfo.maNo);
                let result1 = res1.data.partList;
                if(result1 != null && result1.length >0 && result1[0] != null ){
                    for(let j = 0;j<result1.length;j++){
                        let partInfo = result1[j];
                        if(!partNoArr.includes(partInfo.partCode)){
                            partNoArr.push(partInfo.partCode);
                        }

                    }
                }
            }
            activatedPart(partNoArr).then(res2 =>{
                let data = res2.data;
                if(data != null && data.length > 0){
                    for(let i = 0; i< data.length; i++){
                        let item = data[i];
                        if(item == null){
                            continue;
                        }
                        selectPartList.push(item);
                    }
                    this.$emit('close',selectPartList,remark)

                }
            })
        }else{
            this.$emit('close')
        }
      }
    },
    processData(){
        let selectPartList = [];
        this.selectList.forEach(activityInfo => {
            let partNoArr = [];
            searchDetailedBymaNo(activityInfo.maNo).then(res1 =>{
                res1.data.partList.forEach(partInfo =>{
                    partNoArr.push(partInfo.partCode);
                })
                activatedPart(partNoArr).then(res2 =>{
                    if(res2.data != null && res2.data.length > 0 && res2.data[0] != null){
                        if(selectPartList == null || selectPartList.length <= 0){
                            selectPartList = res2.data;
                         }else{
                             selectPartList.concat(res2.data);

                        }
                    }
                })
            })
        });
        return selectPartList;
    },
    selectChangeHandle(val){
      if(val.length>0){
        this.selectList=val;
        this.isOpen=false;
      }else{
        this.isOpen=true;
        this.selectList=[]
      }

    }
  },

}
</script>
